import { Service, Inject } from "typedi";
import { UserService } from "../UserService";
import { DynamicServiceImpl } from "./DynamicServiceImpl";
import { HomeServiceImpl } from "./HomeServiceImpl";
import { HomeDao } from "../../dao/HomeDao";
import { UserDao } from "../../dao/UserDao";
import { DynamicDao } from "../../dao/DynamicDao";
import jwt from "../../utils/jwt";
import {
  ChangeUserFollowModel,
  getUserData,
  getUserDynamicData,
  getUserUserListData,
  getUserLikeListModel,
  updateUserDataModel
} from "../../model/UserModel";
import { UserEntity } from "../../entity/UserEntity";
import { utils } from "../../utils/utils";
import { DynamicEntity } from "../../entity/DynamicEntity";
import { ThinkEntity } from "../../entity/ThinkEntity";
@Service()
export class UserServiceImpl implements UserService {
  static _isUserObject: { user_PassWord: string } | undefined;
  static _userInfoObject: { user_Phone: string } | undefined;

  @Inject()
  private userDao!: UserDao;

  @Inject()
  private DynamicDao!: DynamicDao;

  @Inject()
  private DynamicServiceImpl!: DynamicServiceImpl;

  @Inject()
  private HomeServiceImpl!: HomeServiceImpl;

  @Inject()
  private HomeDao!: HomeDao;

  public async getUserData(params: getUserData): Promise<any> {
    let userEntity: UserEntity | undefined = await this.userDao.getUserInfoDao(
      params.user_Id
    );
    return await this.changeUser(userEntity!, params.my_Id);
  }

  public async updateUserData(params: updateUserDataModel): Promise<any> {
    return await this.userDao.updateUserData(
      params
    );
  }

  
  public async getUserDynamic(params: getUserDynamicData): Promise<any> {
    let dynamicList: DynamicEntity[] = [];
    dynamicList = await this.DynamicDao.getUserDynamicDao(params.user_Id);
    dynamicList.sort((a: DynamicEntity, b: DynamicEntity): number => {
      return b.dynamic_Time.getTime() - a.dynamic_Time.getTime();
    });
    dynamicList = dynamicList.slice(
      parseInt(params.dynamic_Page) * parseInt(params.dynamic_Count),
      parseInt(params.dynamic_Page) * parseInt(params.dynamic_Count) +
        parseInt(params.dynamic_Count)
    );

    return await Promise.all(
      dynamicList.map(async (e: DynamicEntity, dynamicIndex: number) => {
        return await this.DynamicServiceImpl.changeDynamic(e, params.my_Id);
      })
    ).then((res: DynamicEntity[]) => {
      return res;
    });
  }
  
  public async getPersonalThinkList(params: getUserLikeListModel): Promise<any> {
    let user: UserEntity | undefined = await this.userDao.getUserInfoDao(
      params.my_Id
    );
    let userLikeList: string[] = JSON.parse(
     params.reqIndex == 0?  user!.user_UserInfo.user_LikeList:params.reqIndex == 1?user!.user_UserInfo.user_CollectionList:user!.user_UserInfo.user_RecentVisitList
    ).splice(
      params.page * params.count,
      params.page * params.count + params.count
    );
    return await Promise.all(
      userLikeList.map(async (e: string) => {
        return await this.HomeDao.getOneThinkDao(parseInt(e));
      })
    ).then(async (res: ThinkEntity[]) => {
      return await Promise.all(
        res.map(async (e: ThinkEntity) => {
          return await this.HomeServiceImpl.changeThink(e, params.my_Id);
        })
      ).then((res: ThinkEntity[]) => {
        return res;
      });
    });
  }

  public async getPersonalDynamicList(params: getUserLikeListModel): Promise<any> {

    let user: UserEntity | undefined = await this.userDao.getUserInfoDao(
      params.my_Id
    );
    let dynamicLikeList: string[] = JSON.parse(
      params.reqIndex == 0?  user!.user_UserInfo.user_DynamicLikeList:params.reqIndex == 1?user!.user_UserInfo.user_DynamicCollectionList:user!.user_UserInfo.user_RecentVisitDynamicList
    ).splice(
      params.page * params.count,
      params.page * params.count + params.count
    );
    return await Promise.all(
      dynamicLikeList.map(async (e: string) => {
        return await this.DynamicDao.getOneDynamic(parseInt(e));
      })
    ).then(async (res: DynamicEntity[]) => {
      return await Promise.all(
        res.map(async (e: DynamicEntity) => {
          return await this.DynamicServiceImpl.changeDynamic(e, params.my_Id);
        })
      ).then((res: DynamicEntity[]) => {
        return res.sort((a: DynamicEntity, b: DynamicEntity): number => {
          return b.dynamic_LikeCount - a.dynamic_LikeCount;
        });
      });
    });

    

  }

  public async userLogin(userObj: any): Promise<any> {
    await this.userDao.userLoginDao(userObj.user_Phone);
    UserServiceImpl._userInfoObject = await this.userDao.getUserInfoDao(
      userObj.user_Id
    );
    if (UserServiceImpl._userInfoObject) {
      return {
        user_Data: UserServiceImpl._userInfoObject,
        user_Token: jwt.generateToken(
          UserServiceImpl._userInfoObject.user_Phone
        ),
      };
    } else {
      return "请求错误";
    }
  }

  public async userLogon(params: {
    user_Phone: string;
    user_PassWord: string;
  }): Promise<any> {
    await this.userDao.setOneUser(params);
    return await this.isUser(params);
  }

  public async isUser(params: {
    user_Phone: string;
    user_PassWord: string;
  }): Promise<any> {
    UserServiceImpl._isUserObject = await this.userDao.isUserDao(
      params.user_Phone
    );
    if (UserServiceImpl._isUserObject) {
      if (UserServiceImpl._isUserObject.user_PassWord != params.user_PassWord) {
        return "密码错误";
      } else {
        return await this.userLogin(UserServiceImpl._isUserObject);
      }
    }

    return await this.userLogon(params);
  }

  public async changeUserFollow(params: ChangeUserFollowModel): Promise<any> {
    let userInfo: UserEntity | undefined = await this.userDao.getUserInfoDao(
      parseInt(params.user_Id.toString())
    );
    let followUserInfo: UserEntity | undefined =
      await this.userDao.getUserInfoDao(parseInt(params.follow_Id.toString()));
    let userFollowList: string[];
    userFollowList = JSON.parse(userInfo!.user_UserInfo.user_FollowList);
    let FollowFanList: string[];
    FollowFanList = JSON.parse(followUserInfo!.user_UserInfo.user_FansList);
    if (params.isFollow == "true") {
      userFollowList[userFollowList.length] = params.follow_Id.toString();

      FollowFanList[FollowFanList.length] = params.user_Id.toString();
    } else {
      userFollowList.some((element: string, i: number) => {
        if (element == params.follow_Id.toString()) {
          userFollowList.splice(i, 1);
          return true;
        }
      });

      FollowFanList.some((element: string, i: number) => {
        if (element == params.user_Id.toString()) {
          FollowFanList.splice(i, 1);
          return true;
        }
      });
    }
    await this.userDao.changeFanUser(params.user_Id, params.isFollow);
    await this.userDao.changeFollowUser(params.follow_Id, params.isFollow);
    await this.userDao.setFollowListDao(
      params.user_Id,
      JSON.stringify(userFollowList)
    );
    return await this.userDao.setFansListDao(
      params.follow_Id,
      JSON.stringify(FollowFanList)
    );
  }

  public async changeUser(user: UserEntity, userId: number): Promise<any> {
    let userInfo: UserEntity | undefined = await this.userDao.getUserInfoDao(
      userId
    );
    JSON.parse(userInfo!.user_UserInfo.user_FollowList).some((e: string) => {
      if (parseInt(e) == user.user_Id) {
        user.isFollow = true;
        return true;
      }
    });
    user.user_NickName = utils.uncodeUtf16(user.user_NickName);
    if(user.user_UserInfo){
      if(user.user_UserInfo.user_Autograph)user.user_UserInfo.user_Autograph = utils.uncodeUtf16(user.user_UserInfo.user_Autograph);
    }
    return user;
  }

  public async getUserFollowOrFans(params: getUserUserListData): Promise<any> {
    let userEntity: UserEntity | undefined = await this.userDao.getUserInfoDao(
      params.user_Id
    );
    let userList: UserEntity[] = [];
    let searchList: string[] = JSON.parse(
      params.isFollowData == "true"
        ? userEntity!.user_UserInfo.user_FollowList
        : userEntity!.user_UserInfo.user_FansList
    ).splice(
      parseInt(params.user_Page) * parseInt(params.user_Count),
      parseInt(params.user_Page) * parseInt(params.user_Count) +
        parseInt(params.user_Count)
    );
    userList = await Promise.all(
      searchList.map(async (e: string) => {
        return await this.userDao.getUserInfoDao(parseInt(e));
      })
    ).then((resultData: any[]) => {
      return resultData;
    });

    return await Promise.all(
      userList.map(async (e: UserEntity, userIndex: number) => {
        return await this.changeUser(e, params.my_Id);
      })
    ).then((res: UserEntity[]) => {
      return res;
    });
  }
}
